//
// Created by 123 on 2024/3/6.
//中缀表达式变后缀表达式


#include <iostream>
#include "../0.utils/Char_Stack.cpp"

using namespace std;


//获得操作符的优先级号
int getOpeNum(char a);

//是否是数字
bool isOpe(char ch);
//是否是括号



//中缀表达式变后缀表达式
string change(string str);


int main() {
    string s="((5/(7-(1+1)))*3)-(2+(1+1))";
    string str="5711+-/3*211++-";

    s= change(s);
    cout<<s<<endl;


}

string change(string str) {
    string ans = "";
    LinkStack stack = init_CharStack();
    for (int i = 0; i < str.length(); ++i) {
        if (isOpe(str[i])) {
            char ch = str[i];
            if (ch == '(') {
                push(stack, ch);
                continue;
            }
            if (ch == ')') {
                char ch_bra;
                ch_bra=pop(stack );
                while (ch_bra != '(') {
                    ans += ch_bra;
                    ch_bra=pop(stack);
                }
                continue;
            }
            if (!isEmpty(stack)) {
                char ch_s_top = getTop(stack);
                while (getOpeNum(ch) <= getOpeNum(ch_s_top)) {
                    if(getTop(stack)=='(')
                        break;
                    ch_s_top= pop(stack);
                    ans += ch_s_top;
                    if(isEmpty(stack))
                        break;
                }
            }
            push(stack, ch);
        } else {
            ans += str[i];
        }
    }
    while (!isEmpty(stack)){
        char c;
        c=pop(stack);
        ans+=c;
    }
    return ans;
}


bool isOpe(char ch) {
    return ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '(' || ch == ')';
}


int getOpeNum(char a) {
    if (a == '*' || a == '/' )
        return 1;
    else if(a == '(')
        return 0;
    else
        return -1;
}





